/**
 * 小写字母 a-z 的 ASCII 码范围为 [97, 122][97,122]
 * @param {string} s
 * @return {string}
 */
export var freqAlphabets = function (s) {
  let ans = ''
  for (let i = 0; i < s.length; i++) {
    if (s[i + 2] === '#') {
      ans += String.fromCharCode(parseInt(s.substring(i, i + 3)) + 96)
      i += 2
    } else {
      ans += String.fromCharCode(parseInt(s[i]) + 96)
    }
  }
  return ans
}

/**
 * 两轮替换, 比上边方法快
 * @param {string} s
 * @return {string}
 */
export var freqAlphabets2 = function (s) {
  for (let i = 10; i < 27; i++) {
    s = s.replace(new RegExp(`${i}#`, 'g'), String.fromCharCode(i + 96))
  }
  for (let i = 1; i < 10; i++) {
    s = s.replace(new RegExp(`${i}`, 'g'), String.fromCharCode(i + 96))
  }
  return s
}

export var freqAlphabets3 = function (s) {
  let map = {
    1: 'a',
    2: 'b',
    3: 'c',
    4: 'd',
    5: 'e',
    6: 'f',
    7: 'g',
    8: 'h',
    9: 'i',
    10: 'j',
    11: 'k',
    12: 'l',
    13: 'm',
    14: 'n',
    15: 'o',
    16: 'p',
    17: 'q',
    18: 'r',
    19: 's',
    20: 't',
    21: 'u',
    22: 'v',
    23: 'w',
    24: 'x',
    25: 'y',
    26: 'z',
  }
  let str = ''
  let i = 0
  while (i < s.length) {
    if (s[i + 2] !== '#') {
      str += map[s[i]]
      i++
    } else {
      str += map[s[i] + s[i + 1]]
      i += 3
    }
  }
  return str
}
